<template>
	<view>
		<view class="school">
			<view class="school-name">
				{{$school.schoolName}}
			</view>
			<u-icon name="map-fill" color="white" @click="toPicker()"></u-icon>
		</view>
		<u-picker :show="school.pickerShow" ref="uPicker" :loading="school.loading" :columns="school.provinces"
			@change="changeHandler" @confirm="confirmRegion" @cancel="school.pickerShow=false"></u-picker>
		<u-picker :show="school.show" ref="schoolPicker" :loading="school.loading" :columns="school.schools"
			@confirm="confirmSchoolHandler" @cancel="school.show=false"></u-picker>

	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				school: {
					show: false,
					pickerShow: false,
					loading: false,
					title: '选择学校',
					provinces: [],

					citys: [],
		
					countys: [],

					schools: [
						
					],


				},
				provinces: [],
				citys: [],
				countys: [],
				schools:[],

			}
		},
		computed: {
			...mapState(['$school']),
		},
		components: {
		
		},
		methods: {
			...mapMutations(['changeSchool']),

			toPicker() {
				this.getProvinces();
				this.school.pickerShow = !this.school.pickerShow;

			},
			async getProvinces() {
				let res = await uni.$u.http.get('api/region/pid')
				if (res.success) {

					const storeProvinces = res.data.regions;
					this.provinces = storeProvinces;
					let provinces = [];
					storeProvinces.forEach(item => {

						provinces.push(item.name)

					})

					let citys = await this.getCitys(storeProvinces[0].id);

					let countys = await this.getCountys(this.citys[0].id)

					this.school.provinces = [provinces, citys, countys]



				} else {
					uni.showToast({
						icon:'error',
						title: res.msg,
						duration: 2000
					});
					this.school.pickerShow=false;
					return false;
				}
			},
			async changeHandler(e) {

				const {
					columnIndex,
					value,
					values,
					index,
					picker = this.$refs.uPicker
				} = e

				if (columnIndex === 0) {
					let id = this.provinces[index].id;
					
					let citys = await this.getCitys(id);
					
					this.school.citys = [citys];

					picker.setColumnValues(1, this.school.citys[0])
					let cityId = this.getCityId(citys[0]);
	
					let countys = await this.getCountys(cityId);
					
					this.school.countys = [countys];
					
					picker.setColumnValues(2, this.school.countys[0])

				}
				if (columnIndex == 1) {

					let city = value[1];
					
					let cityId = this.getCityId(city);

					let countys = await this.getCountys(cityId);

					this.school.countys = [countys];

					picker.setColumnValues(2, this.school.countys[0])

				}
				



			},
			getCityId(city) {
				let cityId = 0;
				this.citys.forEach(item => {
					if (item.name == city) {
						cityId = item.id;
					}
				})
				return cityId;
			},
			async getCitys(id) {
				let citys = [];
				let res = await uni.$u.http.get('api/region/pid', {
					params: {
						pid: id
					}
				})
				if(res.success){
					const storeCitys = res.data.regions;
					this.citys = storeCitys;
					storeCitys.forEach(city => {
						citys.push(city.name);
					})
					return citys
					
				}
				else{
					uni.showToast({
						icon:'error',
						title: res.msg,
						duration: 2000
					});
					this.school.pickerShow=false;
					return false;
				}
				
				



			},
			async getCountys(id) {
				let countys = [];
				let res = await uni.$u.http.get('api/region/pid', {
					params: {
						pid: id
					}
				})
				if(res.success){
					const storeCountys = res.data.regions;
					this.countys = storeCountys;
					storeCountys.forEach(city => {
						countys.push(city.name);
					})
					
				}
				else{
					uni.showToast({
						icon:'error',
						title: res.msg,
						duration: 2000
					});
					this.school.pickerShow=false;
					return false;
				}
				
				return countys
			},
		  async	confirmRegion(e){
				let [province,city,county]=e.value
				let data={
					province:province,
					city:city,
					county:county
				}
				
				let res=await  uni.$u.http.post('api/school/search',data);
				if(res.success){
					let storeSchools=[];
					this.school.pickerShow=false;
					this.school.show=true;
					let schools=res.data.schools;
					this.schools=schools;
					schools.forEach(s=>{
						storeSchools.push(s.schoolName)
					})
					this.school.schools=[storeSchools];
				}
				else{
					uni.showToast({
						icon:'error',
						title: res.msg,
						duration: 2000
					});
					this.school.pickerShow=false;
					return false;
				}
				
				
			},
			confirmSchoolHandler(e){
				const [school]=e.value;
				this.schools.forEach(s=>{
					if(s.schoolName==school){
						this.changeSchool(s);
					}
				})
				this.school.show=false;
			}



		},
	
		

	}
</script>

<style>
	.school {
		height: 35px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 10px;
		background-color: #398ADE
	}

	.school-name {
		color: white;
		font-size: 14px;
	}
</style>
